package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.*;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.vo.ExportProductResult;
import cn.itcast.vo.ExportResult;
import com.alibaba.excel.util.CollectionUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Set;

@Service
@Transactional
public class ExportServiceImpl implements ExportService {

    @Autowired
    private ExportDao exportDao;
    @Autowired
    private ContractProductDao contractProductDao;
    @Autowired
    private ExportProductDao exportProductDao;
    @Autowired
    private ExtEproductDao extEproductDao;
    @Autowired
    private ContractDao contractDao;


    @Override
    public Export findById(String id) {
        return exportDao.selectByPrimaryKey(id);
    }

    @Override
    public void save(Export export) {
        //获取id
        String[] contractIds=export.getContractIds().split(",");
        //数组转集合
        List<String> contractIdList = Arrays.asList(contractIds);
        //根据id查找货物
        ContractProductExample contractProductExample = new ContractProductExample();
        contractProductExample.createCriteria().andContractIdIn(contractIdList);
        List<ContractProduct> contractProducts = contractProductDao.selectByExample(contractProductExample);
        for (ContractProduct contractProduct : contractProducts) {
            ExportProduct exportProduct = new ExportProduct();
            //复制相同的元素
            BeanUtils.copyProperties(contractProduct,exportProduct);
            //缺少一个exportId需要赋值
            exportProduct.setExportId(export.getId());
            exportProductDao.insertSelective(exportProduct);
            //购销合同货物下的附件
            List<ExtCproduct> extCproducts = contractProduct.getExtCproducts();
            for (ExtCproduct extCproduct : extCproducts) {
                ExtEproduct extEproduct = new ExtEproduct();
                BeanUtils.copyProperties(extCproduct,extEproduct);
                //缺少两个
                extEproduct.setExportId(export.getId());
                extEproduct.setExportProductId(exportProduct.getId());
                extEproductDao.insertSelective(extEproduct);
            }
        }

        ContractExample contractExample = new ContractExample();
        contractExample.createCriteria().andIdIn(contractIdList);
        List<Contract> contractList = contractDao.selectByExample(contractExample);
        StringBuffer customerContract = new StringBuffer();
        Integer extNum = 0;
        Integer proNum = 0;

        for (Contract contract : contractList) {
            customerContract.append(contract.getCustomName()).append(" ");
            extNum += contract.getExtNum();
            proNum += contract.getProNum();
        }
        export.setCustomerContract(customerContract.toString()); //多个购销合同客户名称用空格断开
        export.setExtNum(extNum); //附件总和
        export.setProNum(proNum); //货物总和

        exportDao.insertSelective(export);
    }

    @Override
    public void update(Export export) {
        //修改报运单以及报运单下货物的数据
        exportDao.updateByPrimaryKeySelective(export);

        //货物
        List<ExportProduct> exportProducts = export.getExportProducts();
        if (exportProducts!=null&&exportProducts.size()>=0) {
            for (ExportProduct exportProduct : exportProducts) {
                exportProductDao.updateByPrimaryKeySelective(exportProduct);
            }
        }
    }

    @Override
    public void delete(String id) {
        Export export = exportDao.selectByPrimaryKey(id);
        ExportProductExample epExample = new ExportProductExample();
        epExample.createCriteria().andExportIdEqualTo(id);
        List<ExportProduct> exportProducts = exportProductDao.selectByExample(epExample);
        for (ExportProduct exportProduct : exportProducts) {
            ExtEproductExample eeExample = new ExtEproductExample();
            eeExample.createCriteria().andExportIdEqualTo(id);
            List<ExtEproduct> extEproducts = extEproductDao.selectByExample(eeExample);
            if (extEproducts!=null&&extEproducts.size()>0) {
                for (ExtEproduct extEproduct : extEproducts) {
                    extEproductDao.deleteByPrimaryKey(extEproduct.getId());
                }
            }
            exportProductDao.deleteByPrimaryKey(exportProduct.getId());
        }
        exportDao.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo findAll(ExportExample example, int page, int size) {
        PageHelper.startPage(page,size);
        List<Export> list = exportDao.selectByExample(example);
        return new PageInfo(list);
    }

    @Override
    public void updateE(ExportResult exportResult) {
        //此处更新两个表
        //export  状态  备注
        Export export = new Export();
        export.setId(exportResult.getExportId());
        export.setState(exportResult.getState());
        export.setRemark(exportResult.getRemark());
        exportDao.updateByPrimaryKeySelective(export);
        //exportProduct  税
        Set<ExportProductResult> products = exportResult.getProducts();
        if (!CollectionUtils.isEmpty(products)){
            for (ExportProductResult product : products) {
                ExportProduct exportProduct = new ExportProduct();
                exportProduct.setId(product.getExportProductId());
                exportProduct.setTax(product.getTax());
                exportProductDao.updateByPrimaryKeySelective(exportProduct);
            }
        }
    }

    @Override
    public List<Export> findByState(Long i) {
        ExportExample exportExample = new ExportExample();
        exportExample.createCriteria().andStateEqualTo(i);
        return exportDao.selectByExample(exportExample);
    }


}
